/**
 * Licensed under the Apache License Version 2.0.
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package net.interknit.knitter.cg.cfg;

import java.util.ArrayList;
import java.util.List;

import net.interknit.knitter.cg.util.Utils;

import org.jdom.Element;

/**
 *
 */
public class CGConfigRootEl
{
	public static final String DEFAULT_GENERATED_SRC_DIR = "generated-src";
	public static final String DEFAULT_ROOT_PACKAGE_NAME = "generated";
	public static final String DEFAULT_AUTHOR = "knitter";

	private String basePackageName;
	private String rootSourceDirectory;
	private ClasspathEl classpath;
	private final List<TargetEl> targets = new ArrayList<TargetEl>();
	private final List<TypeEl> types = new ArrayList<TypeEl>();
	private String author;

	CGConfigRootEl()
	{
		;
	}

	public String getBasePackageName()
	{
		return basePackageName;
	}

	public String getRootSourceDirectory()
	{
		return rootSourceDirectory;
	}

	public ClasspathEl getClasspath()
	{
		return classpath;
	}

	public List<TargetEl> getTargets()
	{
		return targets;
	}

	public TargetEl getTarget(String className)
	{
		for(TargetEl target : targets)
			if(target.getClassName().equals(className))
				return target;

		return null;
	}

	public List<TypeEl> getTypes()
	{
		return types;
	}

	public String getAuthor()
	{
		return author != null && ! "".equals(author) ? author.trim() : "anonymous";
	}

	void setBasePackageName(String b)
	{
		b = b != null ? b.trim() : DEFAULT_ROOT_PACKAGE_NAME;

		if(b.endsWith("."))
			b = b.substring(0, b.length() - 1);

		this.basePackageName = Utils.createValidVariableName(b);
	}

	void setRootSourceDirectory(String d)
	{
		this.rootSourceDirectory = d != null ? d.trim() : DEFAULT_GENERATED_SRC_DIR;
	}

	void setAuthor(String a)
	{
		this.author = a != null ? a.trim() : DEFAULT_AUTHOR;
	}

	public CGConfigRootEl(Element el)
	{
		if(el == null) throw new RuntimeException("cgconfig cannot be empty or null");

		setBasePackageName(el.getAttributeValue("basepackage"));

		setRootSourceDirectory(el.getAttributeValue("srcdir"));

		classpath = new ClasspathEl(el.getChild("classpath"));

		Element targetsEl = el.getChild("targets");
		if(targetsEl != null)
			for(Element targetEl : (List<Element>) targetsEl.getChildren("target"))
				targets.add(new TargetEl(targetEl));

		Element typesEl = el.getChild("types");
		if(typesEl != null)
			for(Element typeEl : (List<Element>) typesEl.getChildren("type"))
				types.add(new TypeEl(typeEl));

		setAuthor(el.getAttributeValue("author"));
	}

	@Override
	public String toString()
	{
		String val = "<cgconfig " +
				"base-package-name=\" " + basePackageName + "\" " +
				"root-source-dir=\"" + rootSourceDirectory + "\">" + "\n";

		val = val + "<targets>" + "\n";
		for(TargetEl target : targets)
			val = val + target.toString() + "\n";
		val = val + "</targets>" + "\n";

		val = val + "<types>" + "\n";
		for(TypeEl type : types)
			val = val + type.toString() + "\n";
		val = val + "</types>" + "\n";

		val = val + "</cgconfig>";

		return val;
	}
}
